﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace jcc.Helpers
{
    public class WebUtility
    {
        public static string GetLatestVersionString(string urlWithVersionText, string AppName)
        {
            string html = DownloadText(urlWithVersionText);
            if (html == null || !html.Contains("{" + AppName + " Current Version: ")) return string.Empty;
            html = html.Substring(html.IndexOf("{" + AppName + " Current Version: ") + ("{" + AppName + " Current Version: ").Length);

            if (!html.Contains("}")) return string.Empty;
            html = html.Substring(0, html.IndexOf("}"));
            return html;
        }

        public static string DownloadText(string URI)
        {
            WebClient client = new WebClient();
            string value = null;
            // int retry = 0;

            //bool success = false;

            try
            {
                value = client.DownloadString(URI);
                if (!String.IsNullOrEmpty(value))
                {
                    //  success = true;
                }

            }
            catch (Exception ex)
            {
                //do something here
            }
            return value;
        }
    }
}
